using DAL = IrisGenerator.Common.DatabaseModel.DataAccess;
using IrisGenerator.Common.Tools;
using System;
namespace IrisGenerator.Common.DatabaseModel {
    
    
    public partial class Database {
        
        private System.Nullable<System.Guid> _dbId;
        
        private string _dbName;
        
        private string _dbFormatedName;
        
        private RequestCollection _requestCollection;
        
        private TableCollection _tableCollection;
        
        public virtual System.Nullable<System.Guid> DbId {
            get {
                return _dbId;
            }
            set {
                _dbId = value;
            }
        }
        
        public virtual string DbName {
            get {
                return _dbName;
            }
            set {
                _dbName = value;
            }
        }
        
        public virtual string DbFormatedName {
            get {
                return _dbFormatedName;
            }
            set {
                _dbFormatedName = value;
            }
        }
        
        public virtual RequestCollection RequestCollection {
            get {
                if ((this._requestCollection == null)) {
                    _requestCollection = IrisGenerator.Common.DatabaseModel.Request.Select_REQUEST_By_DB_ID(this.DbId);
                }
                return this._requestCollection;
            }
        }
        
        public virtual TableCollection TableCollection {
            get {
                if ((this._tableCollection == null)) {
                    _tableCollection = IrisGenerator.Common.DatabaseModel.Table.Select_TABLE_By_DB_ID(this.DbId);
                }
                return this._tableCollection;
            }
        }
        
        private void Clean() {
            this.DbId = null;
            this.DbName = string.Empty;
            this.DbFormatedName = string.Empty;
            this._requestCollection = null;
            this._tableCollection = null;
        }
        
        private void Fill(System.Data.DataRow dr) {
            this.Clean();
            if ((dr["DB_ID"] != System.DBNull.Value)) {
                this.DbId = ((System.Nullable<System.Guid>)(dr["DB_ID"]));
            }
            if ((dr["DB_NAME"] != System.DBNull.Value)) {
                this.DbName = ((string)(dr["DB_NAME"]));
            }
            if ((dr["DB_FORMATED_NAME"] != System.DBNull.Value)) {
                this.DbFormatedName = ((string)(dr["DB_FORMATED_NAME"]));
            }
        }
        
        public static DatabaseCollection GetAll() {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                System.Data.DataSet ds = dbo.DATABASE_Select_All();
                DatabaseCollection collection = new DatabaseCollection();
                if (GlobalTools.IsSafeDataSet(ds)) {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
                        Database obj = new Database();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null)) {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public static Database Load(System.Nullable<System.Guid> DB_ID) {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                System.Data.DataSet ds = dbo.DATABASE_Select_One(DB_ID);
                Database obj = null;
                if (GlobalTools.IsSafeDataSet(ds)) {
                    if ((ds.Tables[0].Rows.Count > 0)) {
                        obj = new Database();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual void Load() {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                System.Data.DataSet ds = dbo.DATABASE_Select_One(this.DbId);
                if (GlobalTools.IsSafeDataSet(ds)) {
                    if ((ds.Tables[0].Rows.Count > 0)) {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual Guid? Insert() {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                return dbo.DATABASE_Insert(this.DbName, this.DbFormatedName);
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
            return null;
        }
        
        public virtual void Delete() {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                dbo.DATABASE_Delete(this.DbId);
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual void Update() {
            DAL.Database dbo = null;
            try {
                dbo = new DAL.Database();
                dbo.DATABASE_Update(this.DbId, this.DbName, this.DbFormatedName);
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
    }
}
